const cloud = require('wx-server-sdk')
const got = require('got');
cloud.init()
const db = cloud.database()

exports.main = async (event, context) => {
  let apiKey = '8qS9tdNISiyIwsfw7jc0pHQI'
  let grantType = 'client_credentials'
  let secretKey = 'aRpQUKI3AkgDhd04BwnDHvtpg5fqeLwu'
  let url = 'https://aip.baidubce.com/oauth/2.0/token?client_id=' + apiKey + '&grant_type=' + grantType + '&client_secret=' + secretKey
  let getResponse = await got(url)
  let responseJson = JSON.parse(getResponse.body);
  console.log(responseJson)
    return await db.collection('token').add({
    data: {

      access_token: responseJson.access_token,
      refresh_token:responseJson.refresh_token,
      scope:responseJson.scope,
      expires_in:responseJson.expires_in,
      refresh_time:new Date().getTime(),
      refresh_date:new Date(),
      session_secret:responseJson.session_secret,
      session_key:responseJson.session_key,
    }
  })
}